#
'''
  update rpki in db
    1. wget http://rpki01.fra2.de.euro-transit.net:8080/export.csv
    2. mv export.csv rpki_data.csv 
    3. run this script
'''
import MySQLdb
import addrutil

infile = open('rpki_data.csv')
infile.next()
conn = MySQLdb.connect(
    host="localhost", user="root", passwd="", db="hijacking", charset="utf8")   
cursor = conn.cursor()

sql = "delete from rpki"
cursor.execute(sql)

rpki_rows_fmt = []
rpki_rows = []
for ln in infile:
  lnarr = ln.split(',')
  asn = int(lnarr[0][2:])
  ipbeg, ipend, pfx_length = addrutil.pfx_str2binrange(
      lnarr[1], ':' in lnarr[1])
  max_length = int(lnarr[2])
  rpki_rows_fmt.append('(%s, %s, %s, %s, %s, %s, %s)')
  rpki_rows.extend((':' in lnarr[1], lnarr[1], pfx_length,
    max_length, ipbeg, ipend, asn))
sql = '''INSERT INTO rpki (is_ipv6, prefix_dec, pfx_length,
    max_length, ipbeg, ipend, asn) VALUES ''' + (', '.join(rpki_rows_fmt))
cursor.execute(sql, rpki_rows)

conn.close()
cursor.close()
infile.close()
